package com.gjh.controller.system;

import com.github.pagehelper.PageInfo;
import com.gjh.controller.BeanController;
import com.gjh.domain.system.Dept;
import com.gjh.domain.system.Role;
import com.gjh.domain.system.User;
import com.gjh.service.system.DeptService;
import com.gjh.service.system.RoleService;
import com.gjh.service.system.UserService;
import com.gjh.utils.MailUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BeanController{

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MailUtils mailUtils;

    @RequestMapping(value = "/list",name = "显示用户列表数据")
    public String findAll(@RequestParam(name ="page",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "10")int pageSize){

        PageInfo page= userService.findByPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",page);
        return "/system/user/user-list";
    }

    @RequestMapping(value = "/toAdd" ,name = "进入新增用户界面")
    public String toAdd(){
        //需要回显所有该企业所有部门的名称
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "/system/user/user-add";
    }

    @RequestMapping(value = "/edit",name = "保存用户数据")
    public String edit(User user){
        if(user.getDeptId().equals("")||user.getDeptName().equals("请选择")){
            user.setDeptId(null);
            user.setDeptName(null);
        }
        if(StringUtils.isEmpty(user.getId())){
            user.setId(UUID.randomUUID().toString());//设置企业id
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getComanyName());
            user.setCreateTime(new Date());

            userService.save(user);
        }else{
            user.setUpdateTime(new Date());
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入用户更新界面")
    public String toUpdate(String id){
        //查询用户信息
        User user=userService.findById(id);
        request.setAttribute("user",user);
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/delete",name = "删除用户")
    public String deleteById(String id){
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/roleList",name = "进入给用户分配角色的界面")
    public String roleList(String id){
        //查询用户对象
        User user = userService.findById(id);
        List<Role> list = roleService.findAll(getCompanyId());
        request.setAttribute("user",user);
        request.setAttribute("roleList",list);
        //查询当前用户的所有角色
        List<String> roleList=userService.findRoleIdsByUserId(id);
        //将集合转成字符串(流式编程)
        String userRoleStr = roleList.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr);
        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name = "给用户分配权限")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }

    @Scheduled(cron="0 0 0 * * ?")
    public void birthdayNotify() throws ParseException {
        System.out.println("当前时间"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        List<User> userList = userService.findAllUser();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String toDay = simpleDateFormat.format(new Date());
//        List<String> userBirthdayList=null;
        for (User user : userList) {
//            userBirthdayList.add(simpleDateFormat.format(simpleDateFormat.parse(user.getBirthday())));
            if (user.getBirthday().contains(toDay)){
                mailUtils.sendEmail(user.getEmail(),"生日快乐","尊贵的会员，saas货代平台，祝您生日快乐！");
            }
        }
    }
}
